from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_wtf.csrf import CSRFProtect
from flask_bootstrap import Bootstrap
from flask_nav import Nav
from flask_nav.elements import *
from flask_login import UserMixin, LoginManager, login_required, login_user, logout_user, current_user
from config import config


csrf = CSRFProtect()
# 实例化Nav
nav = Nav()

app = Flask(__name__)
app.config.from_object('config')
app.config['SECRET_KEY'] = config.SECRET_KEY
app.config['SQLALCHEMY_DATABASE_URI'] = config.SQLALCHEMY_DATABASE_URI
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = config.SQLALCHEMY_TRACK_MODIFICATIONS  # 设置这一项是每次请求结束后 都会自动提交数据库中的变动

# 初始化app
csrf.init_app(app=app)


# 2. 注册 navigation item
nav.register_element('top', Navbar(
    '记账本',
    View('首页', 'index_info'),  # 记账页
    View('交易账号管理', 'account_manager'),
    View('剔除信用卡还款后的消费', 'cost_recenty_info'),
    # Subgroup(
    #     '消费概况',
    #     View('剔除信用卡还款后近一个月日趋势', 'index_info'),
    #     Separator(),
    #     View('剔除信用卡还款后月趋势', 'index_info'),
    #     Separator(),
    #     View('剔除信用卡还款后年趋势', 'index_info'),
    # ),
    View('信用卡还款趋势', 'repayment_recenty_info'),
    View('历史账务查询', 'history_query'),
    View('退出登录', 'logout'),
    ))


login_manager = LoginManager()
login_manager.session_protection = 'strong'
login_manager.login_view = 'login'

# 初始化app
nav.init_app(app=app)
login_manager.init_app(app=app)


boostrap = Bootstrap(app=app)
db = SQLAlchemy(app)
